package cn.stylefeng.guns.modular.pandian.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.pandian.entity.PandianJihua;
import cn.stylefeng.guns.modular.pandian.model.params.PandianDetailedParam;
import cn.stylefeng.guns.modular.pandian.model.params.PandianJihuaParam;
import cn.stylefeng.guns.modular.pandian.model.params.PandianJihuaUserParam;
import cn.stylefeng.guns.modular.pandian.model.result.PandianDetailedResult;
import cn.stylefeng.guns.modular.pandian.model.result.PandianJihuaUserResult;
import cn.stylefeng.guns.modular.pandian.service.PandianDetailedService;
import cn.stylefeng.guns.modular.pandian.service.PandianJihuaService;
import cn.stylefeng.guns.modular.pandian.service.PandianJihuaUserService;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockResult;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;


/**
 * 盘点计划表控制器
 */
@Controller
@RequestMapping("/pandianJihua")
public class PandianJihuaController extends BaseController {

    private String PREFIX = "/pandian/pandianJihua";

    @Autowired
    private PandianJihuaService pandianJihuaService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/pandianJihua.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/pandianJihua_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/pandianJihua_edit.html";
    }

    @Autowired
    private StorageStockService storageStockService;

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PandianJihuaParam pandianJihuaParam) {
    	User user = userService.getcurrentUser();
    	
    	pandianJihuaParam.setSpeed("0");
    	pandianJihuaParam.setCountNum("0");
    	pandianJihuaParam.setAlreadyNum("0");
    	pandianJihuaParam.setAlreadyVolume("0");
    	pandianJihuaParam.setCountVolume("0");
    	
    	BasicWarehouse basicWarehouse = basicWarehouseService.getById(pandianJihuaParam.getWarehouseId());
    	pandianJihuaParam.setWarehouseName(basicWarehouse.getWarehouseName());
    	
    	pandianJihuaParam.setDeptId(user.getDeptId());
    	pandianJihuaParam.setDeliverId(user.getDeliverId());
    	pandianJihuaParam.setStatus("未开始");
    	pandianJihuaParam.setCreateUserName(user.getName());
        this.pandianJihuaService.add(pandianJihuaParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(PandianJihuaParam pandianJihuaParam) {
        PandianJihua detail = this.pandianJihuaService.getById(pandianJihuaParam.getId());
        if(!detail.getStatus().equals("未开始")) {
        	return ResponseData.error("该盘点计划已开盘！");
        }
    	BasicWarehouse basicWarehouse = basicWarehouseService.getById(pandianJihuaParam.getWarehouseId());
    	pandianJihuaParam.setWarehouseName(basicWarehouse.getWarehouseName());
        this.pandianJihuaService.update(pandianJihuaParam);
        return ResponseData.success();
    }

    /**
     * 结束接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PandianJihuaParam pandianJihuaParam) {
    	pandianJihuaParam.setStatus("已结束");
        this.pandianJihuaService.update(pandianJihuaParam);
        return ResponseData.success();
    }

    @Autowired
    private PandianJihuaUserService pandianJihuaUserService;

    @Autowired
    private PandianDetailedService pandianDetailedService;

    /**
     * 开始接口
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResponseData start(PandianJihuaParam pandianJihuaParam) {
    	User user = userService.getcurrentUser();
        PandianJihua detail = this.pandianJihuaService.getById(pandianJihuaParam.getId());

    	PandianJihuaUserParam pandianJihuaUserParam = new PandianJihuaUserParam();
    	pandianJihuaUserParam.setDiskId(Long.parseLong(pandianJihuaParam.getId().toString()));
    	List<PandianJihuaUserResult> list = pandianJihuaUserService.findListBySpec(pandianJihuaUserParam);
    	if(list.size()==0) {
    		return ResponseData.error("请先安排人员");
    	}
        
    	for (PandianJihuaUserResult r : list) {
        	StorageStockParam ssp = new StorageStockParam();
        	ssp.setDeliverId(user.getDeliverId());
        	ssp.setDeptId(user.getDeptId());
        	ssp.setWarehouseId(detail.getWarehouseId());
        	ssp.setAreaId(r.getAreaId());

        	List<StorageStockResult> slist = new ArrayList<StorageStockResult>();
        	if(detail.getDiskType().equals("全盘")) {
        		slist = storageStockService.findListBySpec(ssp,user.getOperateId());
        	} else if(detail.getDiskType().equals("动盘")){
        		slist = storageStockService.findListBySpecDongzuo(ssp,user.getOperateId());
        	}
        	if(slist.size()==0) {
        		return ResponseData.error("暂无待盘库存");
        	}
        	List<PandianDetailedParam> addlist = new ArrayList<PandianDetailedParam>();
        	for (StorageStockResult sr : slist) {
        		PandianDetailedParam pandianDetailedParam = new PandianDetailedParam();
        		pandianDetailedParam.setInventory("待盘");
        		pandianDetailedParam.setDiskId(Long.parseLong(detail.getId().toString()));
        		pandianDetailedParam.setDiskName(detail.getDiskName());
        		pandianDetailedParam.setStockId(Long.parseLong(sr.getId().toString()));
        		pandianDetailedParam.setWarehouseId(sr.getWarehouseId());
        		pandianDetailedParam.setWarehouseName(sr.getWarehouseName());
        		pandianDetailedParam.setAreaId(sr.getAreaId());
        		pandianDetailedParam.setAreaName(sr.getAreaName());
        		pandianDetailedParam.setPositionId(sr.getPositionId());
        		pandianDetailedParam.setPositionNumber(sr.getPositionNumber());
        		pandianDetailedParam.setTrayId(sr.getTrayId());
        		pandianDetailedParam.setTrayNumber(sr.getTrayNumber());
        		pandianDetailedParam.setGoodsId(sr.getGoodsId());
        		pandianDetailedParam.setGoodsName(sr.getGoodsName());
        		pandianDetailedParam.setGoodsNumber(sr.getGoodsNumber());
        		pandianDetailedParam.setNum(sr.getNum());
        		pandianDetailedParam.setVolume(sr.getStockVolume());
        		pandianDetailedParam.setGoodsbatchs(sr.getGoodsbatchs());
        		pandianDetailedParam.setDeptId(sr.getDeptId());
        		pandianDetailedParam.setDeliverId(sr.getDeliverId());
        		addlist.add(pandianDetailedParam);//新增所有 待盘库存明细
    		}
        	if(addlist.size()>0) {
        		pandianDetailedService.saveBatchList(addlist);
        	}
    		
        	PandianDetailedParam pDParam = new PandianDetailedParam();
    		pDParam.setDiskId(Long.parseLong(detail.getId().toString()));
    		pDParam.setAreaId(r.getAreaId());
    		PandianDetailedResult pDResult2 = pandianDetailedService.sumByspec(pDParam);
        	if(pDResult2 != null) {
        		pandianJihuaUserParam.setCountNum(pDResult2.getNum());
        		pandianJihuaUserParam.setCountVolume(pDResult2.getVolume());
        		pandianJihuaUserParam.setId(r.getId());
        		pandianJihuaUserService.update(pandianJihuaUserParam);//更新开盘库区 总数量  总体积
        	}
		}
        
		PandianDetailedParam pDParam2 = new PandianDetailedParam();
		pDParam2.setDiskId(Long.parseLong(detail.getId().toString()));
		PandianDetailedResult pDResult = pandianDetailedService.sumByspec(pDParam2);
    	if(pDResult != null) {
    		pandianJihuaParam.setCountNum(pDResult.getNum());
    		pandianJihuaParam.setCountVolume(pDResult.getVolume());//更新开盘总数量  总体积
    	} else {
    		return ResponseData.error("暂无待盘库存");
    	}
    	pandianJihuaParam.setStatus("进行中");
        this.pandianJihuaService.update(pandianJihuaParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(PandianJihuaParam pandianJihuaParam) {
        PandianJihua detail = this.pandianJihuaService.getById(pandianJihuaParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PandianJihuaParam pandianJihuaParam,String condition) {
    	User user = userService.getcurrentUser();
    	pandianJihuaParam.setDeptId(user.getDeptId());
    	pandianJihuaParam.setDeliverId(user.getDeliverId());
        return this.pandianJihuaService.findPageBySpec(pandianJihuaParam,condition,user.getOperateId());
    }

}


